<?php
  class HomeService{
  	function getHomeById($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $selectTambon, $selectMoo, $selectUnit){
			
			if(in_array($selectTambon, $tambonAccessId)){	
				$where = array();
	
				
				$sql = "
								 SELECT
									pn_degis_contact.aco_tumbol_id, 								
									pn_degis_contact.aco_addr_moo, 
									pn_degis_contact.aco_unit,									
									pn_degis_contact.aco_addr_house_no,									
									COUNT(pn_degis_contact.aco_addr_house_no) AS count_population,
                  pn_degis_tumbol.atb_lat,
                  pn_degis_tumbol.atb_lon,
									IF(INSTR(pn_degis_contact.aco_addr_house_no, '/')=0,0,SUBSTRING_INDEX( pn_degis_contact.aco_addr_house_no , '/', -1 )) AS aco_sub_addr_house_no                  
								 FROM 
									samuidemap.pn_degis_contact,
                  samuidemap.pn_degis_tumbol
								WHERE  
									pn_degis_contact.aco_tumbol_id = $selectTambon	
                AND
                  pn_degis_tumbol.atb_id = $selectTambon 
								AND
								  pn_degis_contact.aco_addr_moo = $selectMoo		
								AND
								  pn_degis_contact.aco_unit = $selectUnit										  				
								GROUP BY 
									pn_degis_contact.aco_addr_house_no
							  ORDER BY CAST(pn_degis_contact.aco_addr_house_no AS SIGNED INTEGER),CAST(aco_sub_addr_house_no AS SIGNED INTEGER)";

					$column = array('tambon_id', 'moo', 'unit', 'house_no', 'count_population', 'lat', 'lon');
					$result = DBUtil::executeSQL($sql);
					$objectArray = DBUtil::marshallObjects ($result, $column);
		
					//print_r($objectArray);
					if($objectArray){
						return $objectArray;
						//$this->setResponse($uid, $provinceIdArray, $districtIdArray, $tambonIdArray, $objectArray, $select, $select_moo, $select_unit);
					}else{
						$this->setResponseNoData();
					}
				}else{
					echo '<?xml version="1.0" encoding="UTF-8"?>
								<datas>
									<data id=0>
										<title>Permission denied , you cannot access this district.</title>
									</data>
								</datas>';    					
				}  		
  	}
  	
  	function arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $objectArray, $selectTambon, $selectMoo, $selectUnit){
  	
    $accessArray['province_access'] = implode(",", $provinceAccessId);
    $accessArray['district_access'] = implode(",", $districtAccessId);
    $accessArray['tambon_access'] = implode(",", $tambonAccessId);  		

  	//Sample $nextxml
	  //$exml = 'http://localhost/zk128/index.php?module=VoteDataCenter&type=service&func=person&province=84&district=8404&tambon=840401,840402,840403,840404,840405,840406&uid=2&select=840406&sm=1&sh=1';
    $iconPath = "http://".$_SERVER['SERVER_NAME']."/modules/VoteDataCenter/pnimages/house.png";
      //Gen xml
      $xml = new DOMDocument('1.0', 'UTF-8');
      $xml->formatOutput = true;
      $xml_node_datas = $xml->createElement("datas");
      $xml->appendChild( $xml_node_datas );
      foreach($objectArray as $key => $val){
        $houseNoId = str_replace("/", "-", $val['house_no']);
        $id = "hn".$selectTambon.$selectMoo.$selectUnit.$houseNoId;
        //echo $id; exit;
        $house_name = "บ้านเลขที่ ".$val['house_no'];

        $xml_node_data = $xml->createElement("data");
        $xml_node_datas->appendChild( $xml_node_data );
        $xml_node_data->setAttribute("id", $id);
          $xml_node_title = $xml->createElement("title",$house_name);
          $xml_node_data->appendChild( $xml_node_title );
          $xml_node_icon = $xml->createElement("icon",$iconPath);
          $xml_node_data->appendChild( $xml_node_icon );
          $xml_node_people = $xml->createElement("people",$val['count_population']);
          $xml_node_data->appendChild( $xml_node_people );
          $xml_node_latitude = $xml->createElement("latitude",$val['lat']);
          $xml_node_data->appendChild( $xml_node_latitude );
          $xml_node_longitude = $xml->createElement("longitude",$val['lon']);
          $xml_node_data->appendChild( $xml_node_longitude );

          $nextxml = "http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?module=VoteDataCenter&amp;type=service&amp;func=person&amp;province=".$accessArray['province_access']."&amp;district=".$accessArray['district_access']."&amp;tambon=".$accessArray['tambon_access']."&amp;uid=".$uid."&amp;st=".$selectTambon."&amp;sm=".$selectMoo."&amp;su=".$selectUnit."&amp;sh=".htmlentities(trim($val['house_no']));
            if ($uid == '6' && $key>='5'){
               $xml_node_xmlpath = $xml->createElement("xmlpath","");
            }else{
              $xml_node_xmlpath = $xml->createElement("xmlpath",$nextxml);
            }

          $xml_node_data->appendChild( $xml_node_xmlpath );
          $xml_node_xmlupdate = $xml->createElement("xmlupdate","HouseData");
          $xml_node_data->appendChild( $xml_node_xmlupdate );
          $xml_node_date = $xml->createElement("date","2010-11-11 20:26:56");
          $xml_node_data->appendChild( $xml_node_date );
      }

      return $xml;
      

  	}

  	function setResponseNoData(){
     echo '<?xml version="1.0" encoding="UTF-8"?> 
          <datas> 
            <data id="0"> 
              <title></title>
              <icon>http://localhost/gthai/home.png</icon>
              <people>0</people>>
              <latitude>0</latitude> 
              <longitude>0</longitude> 
              <xmlpath></xmlpath>
              <xmlupdate>update.xml</xmlupdate>
              <date>2010-11-11 20:26:56</date>
            </data>
          </datas>';
      pnShutDown();
  	}
  	
  	
  	function renderXMLResponse($xmlObject){
      echo $xmlObject->saveXML();
  	}
  		      				        	
  	function saveXMLResponse($xmlDirectoryPath, $xmlFileName, $xmlObject){      
      $xmlObject->save($xmlDirectoryPath.$xmlFileName);	
  	}    	
  }
?>